package com.runmi.webchat.aftersale.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;

public class NumberUtil {
	
	 public final static BigDecimal PRICERATE = new BigDecimal(100);
	 /**
     * 将价格（或金额、额度等）统一处理后提供前台显示
     * @param data
     * @return
     */
    public static String forPriceView(Long data) {
        if (data == null)
            return "";
        return new BigDecimal(data).divide(PRICERATE, 2, RoundingMode.HALF_DOWN).toString();
    }
    
	 /**
     * 将价格（或金额、额度等）统一处理后提供前台显示
     * @param data
     * @return
     */
    public static String forWeightView(Long data) {
        if (data == null)
            return "";
        return new BigDecimal(data).divide(new BigDecimal(1000), 3, RoundingMode.HALF_DOWN).toString();
    }
    
    public static String forPriceView(BigDecimal data) {
        if (data == null)
            return "";
        return data.divide(PRICERATE, 2, RoundingMode.HALF_DOWN).toString();
    }
    
    /**
     * 将价格（或金额、额度等）统一处理后保存到数据库
     * @param data
     * @return
     */
    public static Long forPriceDb(Long data) {
        if (data == null)
            return 0L;
        return PRICERATE.multiply(new BigDecimal(data)).longValue();
    }

    public static Long forPriceDb(BigDecimal data) {
        if (data == null)
            return 0L;
        return PRICERATE.multiply(data).longValue();
    }

    public static Long forPriceDb(String data) {
        if (StringUtil.isBlank(data))
            return 0L;
        return PRICERATE.multiply(new BigDecimal(data)).longValue();
    }

	/**
	 * 数字*金额
	 * @param num
	 * @param price
	 * @return
	 */
	public static double numberMathPrice(Integer num,Double price){
		num=num==null?0:num;
		price=price==null?(double)0:price;
		double result=0;
		Integer price100= (int)Math.rint(price *(double)100);
		Integer total=price100 * num; 
		result = new BigDecimal((float)total / 100).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();  
		return result;
	}
	
	public static Integer doubleToInteger(String price){
		Integer price100=null;
		if(StringUtil.isBlank(price)){
			return null;
		}
		try{
			Double priced=Double.parseDouble(price);
			price100= (int)Math.rint(priced *(double)100);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return price100;
	}
	public static Double formatDouble(Object object){
		Double result=null;
		if(null==object){
			return result;
		}		
		if(object.getClass().getName().equals(Double.class.getName())){
			double f=(double)object;
			result = new BigDecimal(f).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();  
		}
		if(object.getClass().getName().equals(BigDecimal.class.getName())){
			BigDecimal f=(BigDecimal)object;
			result = f.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();  
		}
		if(object.getClass().getName().equals(Integer.class.getName())){
			int f=(int)object;
			result = new BigDecimal(f).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();  
		}  
		return result;
	}
	public static Integer formatInteger(Object  object){
		Integer result=null;
		if(null==object){
			return result;
		}	
		if(object.getClass().getName().equals(Double.class.getName())){
			double f=(double)object;
			result = new BigDecimal(f).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();  
		}
		if(object.getClass().getName().equals(BigDecimal.class.getName())){
			BigDecimal f=(BigDecimal)object;
			result = f.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();  
		}
		if(object.getClass().getName().equals(Integer.class.getName())){
			int f=(int)object;
			result = new BigDecimal(f).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();  
		}
		
		return result;
	}
	
	public static double PriceAddPrice(Double price1,Double price2){
		double result=0;
		price1=price1==null?(double)0:price1;
		price2=price2==null?(double)0:price2;	
		result = new BigDecimal(price1 + price2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();  
		return result;
	}
	
	public static Integer StringToInt(String value){
		Integer result=null;
		try{
			result=Integer.parseInt(value);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	public static Integer StringToInt(String value,Integer defaultValue){
		Integer result=null;
		try{
			result=Integer.parseInt(value);
		}catch(Exception ex){
			ex.printStackTrace();
			result=defaultValue;
		}
		return result;
	}
	/**
	 * 整数除法并且四舍入取整
	 * @param value1
	 * @param value2
	 * @return
	 */
	public static Integer diveRint(Integer value1,Integer value2){
		double c= (double)value1 / value2;
		BigDecimal de= new BigDecimal(c).setScale(0, BigDecimal.ROUND_HALF_UP);
		return de.intValue();
	}
	
	public static String getAmountString(double d) {
		NumberFormat format = NumberFormat.getInstance(Locale.CHINA);

		format.setMaximumFractionDigits(2);
		format.setMinimumFractionDigits(2);

		return format.format(d);
	}
	
}
